package com.medicine.manage.service.impl;

import java.util.List;
import com.medicine.common.utils.DateUtils;
import com.medicine.common.utils.SecurityUtils;
import com.medicine.manage.block.entity.WeBaseResult;
import com.medicine.manage.block.service.IBlockOrderDetailService;
import com.medicine.manage.block.service.IBlockOrderService;
import com.medicine.manage.domain.OrderProcess;
import com.medicine.manage.domain.vo.KeyValue;
import com.medicine.manage.service.IOrderProcessService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.Map;

import com.medicine.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.medicine.manage.domain.OrderDetail;
import com.medicine.manage.mapper.OrderMapper;
import com.medicine.manage.domain.Order;
import com.medicine.manage.service.IOrderService;

/**
 * 订单Service业务层处理
 * 
 * @author admin
 * @date 2024-10-13
 */
@Service
public class OrderServiceImpl implements IOrderService 
{
    @Autowired
    private OrderMapper orderMapper;


    @Autowired
    private IBlockOrderService blockOrderService;

    @Autowired
    private IBlockOrderDetailService blockOrderDetailService;
    @Autowired
    private OrderProcessServiceImpl orderProcessServiceImpl;

    /**
     * 查询订单
     * 
     * @param id 订单主键
     * @return 订单
     */
    @Override
    public Order selectOrderById(Long id)
    {
        return orderMapper.selectOrderById(id);
    }

    @Override
    public List<Order> selectOrderList(Order order) {
        return orderMapper.selectOrderList(order);
    }


    /**
     * 查询订单列表
     * 
     * @param order 订单
     * @return 订单
     */

    /**
     * 新增订单
     * 
     * @param order 订单
     * @return 结果
     */
    @Transactional
    @Override
    public int insertOrder(Order order)
    {
        order.setCreateBy(SecurityUtils.getLoginUser().getUserId().toString());
        order.setCreateTime(DateUtils.getNowDate());
        int rows = orderMapper.insertOrder(order);
        blockOrderService.insertOrder(order, order.getDeptId(), SecurityUtils.getLoginUser().getUserId());

        insertOrderDetail(order);
        return rows;
    }

    /**
     * 修改订单
     * 
     * @param order 订单
     * @return 结果
     */
    @Transactional
    @Override
    public int updateOrder(Order order)
    {
        if(order.getStatus() == 6L){
            OrderProcess orderProcess = orderProcessServiceImpl.selectOrderProcessByOrderAndType(order.getId(), IOrderProcessService.EXPRESS);
            order.setUpdateBy(orderProcess.getCreateBy());
            order.setUpdateTime(DateUtils.getNowDate());
            int result = orderMapper.updateOrder(order);
            Order o = this.selectOrderById(order.getId());
            blockOrderService.updateOrder(o, o.getDeptId(), new Long(orderProcess.getCreateBy()));
            return result;
        }
        order.setUpdateBy(SecurityUtils.getLoginUser().getUserId().toString());
        order.setUpdateTime(DateUtils.getNowDate());

        if(order.getOrderDetailList()!=null && order.getOrderDetailList().size()>0){
            orderMapper.deleteOrderDetailByOrderId(order.getId());
            insertOrderDetail(order);
//            blockOrderDetailService.insertOrderDetailList(order.getOrderDetailList(), SecurityUtils.getLoginUser().getDeptId(), SecurityUtils.getLoginUser().getUserId());
        }
        int result = orderMapper.updateOrder(order);
        Order o = this.selectOrderById(order.getId());
        blockOrderService.updateOrder(o, o.getDeptId(), SecurityUtils.getLoginUser().getUserId());
        return result;
    }

    /**
     * 批量删除订单
     * 
     * @param ids 需要删除的订单主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteOrderByIds(Long[] ids)
    {
        orderMapper.deleteOrderDetailByOrderIds(ids);
        return orderMapper.deleteOrderByIds(ids);
    }

    /**
     * 删除订单信息
     * 
     * @param id 订单主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteOrderById(Long id)
    {
        orderMapper.deleteOrderDetailByOrderId(id);
        return orderMapper.deleteOrderById(id);
    }

    @Override
    public Integer orderCountAll() {
        return Integer.parseInt(String.valueOf(orderMapper.orderCountAll().get("count")));
    }

    @Override
    public Integer orderCountOneDay() {

        return Integer.parseInt(String.valueOf(orderMapper.orderCountOneDay().get("count")));
    }

    @Override
    public Integer orderCountForNotComplete() {
        return Integer.parseInt(String.valueOf(orderMapper.orderCountForNotComplete().get("count")));
    }

    @Override
    public Integer orderCountForComplete() {
        return Integer.parseInt(String.valueOf(orderMapper.orderCountForComplete().get("count")));
    }

    @Override
    public List<KeyValue> orderCountForThirtyDay() {
        return orderMapper.orderCountForThirtyDay();
    }

    /**
     * 新增订单详情信息
     * 
     * @param order 订单对象
     */
    public void insertOrderDetail(Order order)
    {
        List<OrderDetail> orderDetailList = order.getOrderDetailList();
        Long id = order.getId();
        if (StringUtils.isNotNull(orderDetailList))
        {
            List<OrderDetail> list = new ArrayList<OrderDetail>();
            for (OrderDetail orderDetail : orderDetailList)
            {
                orderDetail.setCreateTime(DateUtils.getNowDate());
                orderDetail.setCreateBy(SecurityUtils.getLoginUser().getUserId().toString());
                orderDetail.setOrderId(id);
                list.add(orderDetail);
            }
            if (list.size() > 0)
            {
                orderMapper.batchOrderDetail(list);

                WeBaseResult result = blockOrderDetailService.insertOrderDetailList(list, SecurityUtils.getLoginUser().getDeptId(), SecurityUtils.getLoginUser().getUserId());
                System.out.println(result);
            }
        }
    }
}
